{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e040ecfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1035d800",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户、通行证\n",
    "API_key = 'Ikxs3Cb7_ztPlpAi-Kn2MY0-hPWEqYRU'\n",
    "API_secret = 'kTSuS0xRCqb0lO1UJOJTN9OFJ1NVR0B_'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://pic4.zhimg.com/v2-7b5792f1cd1a3c27a732a1b1c5ffaa32_720w.jpg?source=172ae18b'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "053b9fc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b84c9e83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648378321,e537de6d-6720-494e-ae95-1095a63ee1fb\",\"time_used\":231,\"faces\":[{\"face_token\":\"5734c07019277511fea6e1033f982b27\",\"face_rectangle\":{\"top\":139,\"left\":120,\"width\":234,\"height\":234}}],\"image_id\":\"oV28UdGq5vYBYo/P4Txrjw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1bbccf09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648378321,e537de6d-6720-494e-ae95-1095a63ee1fb',\n",
       " 'time_used': 231,\n",
       " 'faces': [{'face_token': '5734c07019277511fea6e1033f982b27',\n",
       "   'face_rectangle': {'top': 139, 'left': 120, 'width': 234, 'height': 234}}],\n",
       " 'image_id': 'oV28UdGq5vYBYo/P4Txrjw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "73fb323d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5734c07019277511fea6e1033f982b27'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b6c9217",
   "metadata": {},
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4e6e26c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "# 1. api账户、通行证\n",
    "API_key = 'Ikxs3Cb7_ztPlpAi-Kn2MY0-hPWEqYRU'\n",
    "API_secret = 'kTSuS0xRCqb0lO1UJOJTN9OFJ1NVR0B_'\n",
    "\n",
    "# 2. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 3. 发起请求\n",
    "r_analyze = requests.post(analyze_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6aaef3ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0b006eb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 253,\n",
       " 'request_id': '1648379016,84ba3262-10b1-4cd3-bdbf-4344488f169f',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.0,\n",
       "     'neutral': 99.999,\n",
       "     'disgust': 0.0,\n",
       "     'anger': 0.0,\n",
       "     'surprise': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.0},\n",
       "    'beauty': {'female_score': 82.02, 'male_score': 81.13},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 28},\n",
       "    'mouthstatus': {'close': 99.663,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.336,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 1.776,\n",
       "     'stain': 39.482,\n",
       "     'acne': 44.862,\n",
       "     'health': 2.85},\n",
       "    'headpose': {'yaw_angle': -11.022309,\n",
       "     'pitch_angle': 5.981852,\n",
       "     'roll_angle': -0.009773361},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.358},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.358},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.358}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.043},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.001,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.999,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.004,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.011,\n",
       "      'no_glass_eye_open': 99.979,\n",
       "      'normal_glass_eye_close': 0.001,\n",
       "      'dark_glasses': 0.005}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 75.554},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.501,\n",
       "      'vector_z_component': 0.966,\n",
       "      'vector_x_component': -0.059,\n",
       "      'vector_y_component': -0.254,\n",
       "      'position_y_coordinate': 0.41},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.523,\n",
       "      'vector_z_component': 0.95,\n",
       "      'vector_x_component': -0.07,\n",
       "      'vector_y_component': -0.304,\n",
       "      'position_y_coordinate': 0.397}}},\n",
       "   'face_rectangle': {'width': 234, 'top': 139, 'left': 120, 'height': 234},\n",
       "   'face_token': '5734c07019277511fea6e1033f982b27'}]}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a339f70e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1bb1bcb",
   "metadata": {},
   "source": [
    "#  人脸检测获取toekn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "10e0e2b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户、通行证\n",
    "API_key = 'Ikxs3Cb7_ztPlpAi-Kn2MY0-hPWEqYRU'\n",
    "API_secret = 'kTSuS0xRCqb0lO1UJOJTN9OFJ1NVR0B_'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fstatic.jstv.com%2Fgather%2Fhl_dp%2F20210610%2F13%2F11007646649075554597.jpg&refer=http%3A%2F%2Fstatic.jstv.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650972483&t=956a4f73a75496d838ccf2f18b56e5a6'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2cb92d16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'66a8174f95124e8525dd6a06b10eaac5'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8033990d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户、通行证\n",
    "API_key = 'Ikxs3Cb7_ztPlpAi-Kn2MY0-hPWEqYRU'\n",
    "API_secret = 'kTSuS0xRCqb0lO1UJOJTN9OFJ1NVR0B_'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img2.baidu.com/it/u=2659562911,580809381&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7bc3a371",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'240055ea7b28af31cc005e1aebc076df'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80a0495c",
   "metadata": {},
   "source": [
    "## 3.2人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "63c669dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'Ikxs3Cb7_ztPlpAi-Kn2MY0-hPWEqYRU'\n",
    "API_secret = 'kTSuS0xRCqb0lO1UJOJTN9OFJ1NVR0B_'\n",
    "\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1901e678",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9643501d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648380787,e657df11-668d-4b4f-a390-a32bf6705e67',\n",
       " 'time_used': 474,\n",
       " 'confidence': 88.457,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a117bfe1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
